<template>
  <div class="member">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>会员管理</el-breadcrumb-item>
      <el-breadcrumb-item>会员列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box_card">
      <!-- 顶部搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            class="input-with-select"
            placeholder="车牌号搜索"
            v-model="car_number_search"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="carnumber()"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 添加会员 -->
        <el-col :span="6">
          <el-button type="primary" @click="isvipdag = true">添加会员</el-button>
        </el-col>
      </el-row>
      <!-- 车辆信息列表区 -->
      <el-table :data="viplist" border stripe class="tabel_box">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="车牌号" prop="car_number"></el-table-column>
        <el-table-column label="月卡" prop="car_state">
          <template slot-scope="scope">
            <el-switch
              @change="modifymonthcard(scope.row.vip_id, scope.$index)"
              :value="scope.row.vip_month_card.toLowerCase() === 'true'"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="月卡开卡时间" prop="vip_open_month_card_time">
          <template slot-scope="scope">
            <span>{{
              scope.row.vip_open_month_card_time !== "None"
                ? scope.row.vip_open_month_card_time
                : "未开卡"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="年卡" prop="car_state">
          <template slot-scope="scope">
            <el-switch
              @change="modifyyearcard(scope.row.vip_id, scope.$index)"
              :value="scope.row.vip_year_card.toLowerCase() === 'true'"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="年卡开卡时间" prop="vip_open_year_card_time">
          <template slot-scope="scope">
            <span>{{
              scope.row.vip_open_year_card_time !== "None"
                ? scope.row.vip_open_year_card_time
                : "未开卡"
            }}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <div class="page_box" v-if="ispage">
        <el-pagination
          :background="true"
          :page-count="pager_count"
          @current-change="handleCurrentChange"
          :layout="col"
          :total="count_total"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- -----添加会员对话框------ -->
    <el-dialog title="提示" :visible.sync="isvipdag" width="30%" v-loading="viplod">
      <el-input
        v-model="car_number_val"
        placeholder="请输入车牌号"
        maxlength="10"
      ></el-input>

      <el-select
        v-model="viptype"
        placeholder="请选择"
        style="margin-top: 20px"
      >
        <el-option
          v-for="item in vipselect"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isvipdag = false">取 消</el-button>
        <el-button type="primary" @click="addcarvip()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      viplist: [], //会员数据信息列表
      car_number_search: "", //车牌号搜索
      pager_count: 1, // 当前页码
      count_total: 0, // 总页数
      col: "prev, pager, next, jumper,slot", //开启的配置
      ispage: true, // 是否显示分页
      isvipdag: false, //是否显示添加会员对话框
      car_number_val: "", //添加会员车牌号
      viptype: "", //会员类型
      viplod:false,//会员加载框
      vipselect: [
        {
          value: "month",
          label: "月卡",
        },
        {
          value: "year",
          label: "年卡",
        },
      ],
    };
  },
  mounted() {
    this.iscardlose(); //判断会员是否过期
    setTimeout(() => {
      this.getvipInfo(); //获取会员信息列表
    }, 300);
  },
  watch: {
    // 如果只有1页，则不显示分页按钮
    count_total(newVal) {
      if (newVal <= 10) {
        this.ispage = false;
      }
    },
  },
  methods: {
    funvipinfo(parameter) {
      this.$getapi("/vip/get", parameter, "json")
        .then((res) => {
          if (res.code === 200) {
            this.viplist = res.data;
            this.count_total = res.count * 10;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((error) => {
          this.$message.error(error);
        });
    },
    // 获取会员信息列表
    getvipInfo() {
      let par = {
        page: this.pager_count,
      };
      this.funvipinfo(par);
    },
    // 车牌号搜索
    carnumber() {
      let par = { car_number: this.car_number_search };
      this.funvipinfo(par);
    },
    // 页数跳转
    handleCurrentChange(val) {
      this.pager_count = val;
      this.getvipInfo();
    },
    // 判断年卡月卡是否过期，过期自动重置
    iscardlose() {
      this.$postapi("/vip/modify/vip_month", "", "json");
      this.$postapi("/vip/modify/vip_year", "", "json");
    },
    // 修改月卡会员状态
    modifymonthcard(vip_id, i) {
      this.$postapi("/vip/new/month_card", { vip_id: vip_id }, "json")
        .then((res) => {
          if (res.code === 200) {
            if (res.static === "True") {
              this.viplist[i].vip_month_card = res.static;
              this.getvipInfo();
            } else if (res.static === "False") {
              this.viplist[i].vip_month_card = res.static;
              this.getvipInfo();
            }
            this.$message.success(res.msg);
          } else if (res.code === 404) {
            this.$message.warning(res.msg);
          } else {
            this.$$message.error(res.msg);
          }
        })
        .catch((error) => {
          this.$message.error(error);
        });
    },
    // 修改年卡会员状态
    modifyyearcard(vip_id, i) {
      this.$postapi("/vip/new/year_card", { vip_id: vip_id }, "json")
        .then((res) => {
          if (res.code === 200) {
            if (res.static === "True") {
              this.viplist[i].vip_year_card = res.static;
              this.getvipInfo();
            } else if (res.static === "False") {
              this.viplist[i].vip_year_card = res.static;
              this.getvipInfo();
            }
            this.$message.success(res.msg);
          } else if (res.code === 404) {
            this.$message.warning(res.msg);
          } else {
            this.$$message.error(res.msg);
          }
        })
        .catch((error) => {
          this.$message.error(error);
        });
    },
    // 为车牌添加会员
    addcarvip() {
      if (this.car_number_val.length < 7)
        return this.$message.warning("车牌号长度不正确");
      if (this.viptype === "") return this.$message.warning("请选择会员类型");
      this.viplod=true//显示会员加载框
      this.$postapi(
        "/vip/admin/add",
        { car_number: this.car_number_val, type: this.viptype },
        "json"
      )
        .then((res) => {
          if (res.code === 200) {
            this.isvipdag = false;
            this.$message.success(res.msg);
            this.viplod=false
            this.getvipInfo()// 刷新会员列表
            this.car_number_val = "";
            this.viptype = "";
          } else if (res.code === 404) {
            this.$message.warning(res.msg);
            this.viplod=false
          } else {
            this.$message.error(res.msg);
            this.viplod=false
          }
        })
        .catch((error) => {
          this.$message.error(error);
          this.viplod=false
        });
    },
  },
};
</script>

<style lang="less" scoped>
.member {
  .box_card {
    margin-top: 20px;
    .tabel_box {
      font-size: 14px;
    }
    .page_box {
      margin-top: 20px;
    }
  }
  .el-row {
    margin-bottom: 10px;
  }
}
</style>